package com.shenbao.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSON;
import com.shenbao.beans.Department;
import com.shenbao.beans.ExpertAdvice;
import com.shenbao.beans.MoneySource;
import com.shenbao.beans.ProjectApply;
import com.shenbao.beans.ProjectState;
import com.shenbao.beans.SysRole;
import com.shenbao.beans.SysUser;
import com.shenbao.service.IDepartmentService;
import com.shenbao.service.IExpertAdviceService;
import com.shenbao.service.IMoneySourceService;
import com.shenbao.service.IProjectApplyService;
import com.shenbao.service.IProjectStateService;
import com.shenbao.service.ISysRoleService;
import com.shenbao.service.ISysUserService;
import com.shenbao.util.JsonMessage;
import com.shenbao.util.SysPagingUtil;
import com.shenbao.vo.ExpertAdviceVO;
import com.shenbao.vo.ProjectApplyVO;
import com.shenbao.vo.SysUserVO;

@Controller
@RequestMapping("/project")
public class ProjectApplyController {
	
	@Resource
	private IProjectApplyService iProjectApplyService;
	
	@Resource
	private ISysUserService iSysUserService;
	
	@Resource
	private IDepartmentService iDepartmentService;
	
	@Resource 
	private IMoneySourceService iMoneySourceService;
	
	@Resource
	private IProjectStateService iProjectStateService;
	
	@Resource
	private ISysRoleService iSysRoleService;
	
	@Resource
	private IExpertAdviceService iExpertAdviceService;
	
	
	@RequestMapping("/gotoshowallapply.do")
	public String gotoshowallapply(){
		return "/projectapply/showallapply";
	}
	
	@RequestMapping("/showallapply.do")
	public void showallapply(HttpServletRequest request,HttpServletResponse response) throws IOException{
		String pageSize =String.valueOf(request.getParameter("rows"));
		String pageNum = String.valueOf(request.getParameter("page"));
		
		String applyername = request.getParameter("applyername");
		String projectstateid = request.getParameter("projectstateid");
		if(projectstateid != null && projectstateid.equals("-1")){
			projectstateid = null;
		}
		if(applyername != null){
			if(applyername.equals("")){
				applyername = null;
			}
		}
		
		
		Map<String, String> map = new HashMap<String, String>();
		map.put("pageSize", pageSize);
		map.put("pageNum", pageNum);
		map.put("applyername", applyername);
		map.put("projectstateid", projectstateid);

		Subject subject = SecurityUtils.getSubject();
		SysUser sysUser = iSysUserService.getUserByLoginName((String)subject.getPrincipal());
		
		//**本系统每个用户只会有一个角色
		List<SysRole> roles = iSysRoleService.getUserRolesByUserId(sysUser.getId());
		SysRole sysRole = roles.get(0);
		// 45评审专家  暂时用不到
		// 42教务处 8 系统管理员
		if(sysRole.getId().toString().equals("8") ||sysRole.getId().toString().equals("45") ||sysRole.getId().toString().equals("42") ){
			// 为空 表明可以查看所有的
		}
		//44 项目申报人 
		else if(sysRole.getId().toString().equals("44")){
			// 只可以查看自己的
			map.put("userid", String.valueOf(sysUser.getId()));
		}
		else if(sysRole.getId().toString().equals("43") || sysRole.getId().toString().equals("46")){ //系部主任 系部管理员
			//可以查看本系的
			map.put("departmentid", sysUser.getDepartment());
		}
		

		
		List<ProjectApplyVO> projectApply = iProjectApplyService.getProjectApplyByParam(map);
		int count = iProjectApplyService.getProjectApplyCountByParam(map);
		
		SysPagingUtil sysPagingUtil = new SysPagingUtil();
		
		sysPagingUtil.setTotal(String.valueOf(count));
		sysPagingUtil.setRows(projectApply);

		String jsondata = JSON.toJSONString(sysPagingUtil);

		response.setContentType("html/json;charset=UTF-8");
		response.getWriter().write(jsondata);
	}
	
	@RequestMapping("/gotoaddapply.do")
	public String gotoaddapply(Model model){
		// 获取当前用户的 信息 id等 
		Subject subject = SecurityUtils.getSubject();
		String username = (String) subject.getPrincipal();
		SysUser sysUser = iSysUserService.getUserByLoginName(username);
		
		List<Department> departments = iDepartmentService.getalldepartment();
		List<MoneySource> moneySources = iMoneySourceService.getallmoneysource();
		List<ProjectState> projectStates = iProjectStateService.getallprojectstates();
		
		model.addAttribute("sysuser", sysUser);
		model.addAttribute("departments", departments);
		model.addAttribute("moneysources", moneySources);
		model.addAttribute("projectstates", projectStates);
		
		return "/projectapply/addapply";
	}
	
	@RequestMapping(value="/addapply.do", method = RequestMethod.POST)
	public void addapply(HttpServletRequest request ,HttpServletResponse response,@RequestParam("data") String data,@RequestParam("attachment") MultipartFile file) throws IOException{
		data = URLDecoder.decode(URLDecoder.decode(data,"UTF-8"),"UTF-8"); 
		ProjectApply projectApply = JSON.parseObject(data,ProjectApply.class); 
		String fileName = file.getOriginalFilename();
		Date d=new Date();
		SimpleDateFormat sf=new SimpleDateFormat("yyyyMMddhhmmss");
		if(file.getOriginalFilename() != ""){
			projectApply.setAttachment(projectApply.getUserid()+"_"+sf.format(d)+"_"+fileName);
		}
		
		//ProjectApplyVO projectApply = (ProjectApplyVO) JSON.parse(data);
		int count = iProjectApplyService.insertSelective(projectApply);//iDeviceDocService.insertSelective(deviceDoc);
		String path = request.getSession().getServletContext().getRealPath("/");
		File targetFile = new File(path, projectApply.getUserid()+"_"+sf.format(d)+"_"+fileName);
		file.transferTo(targetFile); 
		
		
		JsonMessage jsonMessage = new JsonMessage();
		if(count == 1){
			jsonMessage.setKey("pass");
		}
		response.setContentType("text/html; charset=UTF-8");
		response.getWriter().write(JSON.toJSONString(jsonMessage));
	}
	
	/**
	 * 修改 查看项目申请书
	 * @param model
	 * @return
	 */
	@RequestMapping("/gotomodifyapply.do")
	public String gotomodifyapply(HttpServletRequest request, Model model){
		String ismodify = request.getParameter("ismodify");
		String id = request.getParameter("id");
		ProjectApply projectApply = iProjectApplyService.selectByPrimaryKey(Integer.valueOf(id));
		SysUser sysUser = iSysUserService.selectByPrimaryKey(projectApply.getUserid());
		
		List<Department> departments = iDepartmentService.getalldepartment();
		List<MoneySource> moneySources = iMoneySourceService.getallmoneysource();
		List<ProjectState> projectStates = iProjectStateService.getallprojectstates();
		
		model.addAttribute("sysuser", sysUser);
		model.addAttribute("departments", departments);
		model.addAttribute("moneysources", moneySources);
		model.addAttribute("projectstates", projectStates);
		model.addAttribute("projectapply", projectApply);

		if(ismodify.equals("yes")){ //修改
			model.addAttribute("ismodify", "yes");
		}else {//仅仅是
			model.addAttribute("ismodify", "chakan");
		}
		return "/projectapply/addapply";
	}
	
	/**
	 * 查看项目申请书
	 * @param model
	 * @return
	 */
	@RequestMapping("/gotoshowapply.do")
	public String gotoshowapply(Model model){
		
		
		
		// 获取当前用户的 信息 id等 
		Subject subject = SecurityUtils.getSubject();
		String username = (String) subject.getPrincipal();
		SysUser sysUser = iSysUserService.getUserByLoginName(username);
		
		List<Department> departments = iDepartmentService.getalldepartment();
		List<MoneySource> moneySources = iMoneySourceService.getallmoneysource();
		List<ProjectState> projectStates = iProjectStateService.getallprojectstates();
		
		model.addAttribute("sysuser", sysUser);
		model.addAttribute("departments", departments);
		model.addAttribute("moneysources", moneySources);
		model.addAttribute("projectstates", projectStates);
		
		return "/projectapply/addapply";
	}
	
/*	@RequestMapping("/updateapply.do")
	public void updateapply(@RequestBody ProjectApply projectApply,HttpServletResponse response) throws IOException{
		int count = iProjectApplyService.updateByPrimaryKeySelective(projectApply);//iDeviceDocService.insertSelective(deviceDoc);
		JsonMessage jsonMessage = new JsonMessage();
		
		if(count == 1){
			jsonMessage.setKey("pass");
		}
		response.setContentType("text/html; charset=UTF-8");
		response.getWriter().write(JSON.toJSONString(jsonMessage));
	}*/
	
	@RequestMapping(value="/updateapply.do", method = RequestMethod.POST)
	public void updateapply(HttpServletRequest request ,HttpServletResponse response,@RequestParam("data") String data,@RequestParam("attachment") MultipartFile file) throws IOException{
		data = URLDecoder.decode(URLDecoder.decode(data,"UTF-8"),"UTF-8"); 
		ProjectApply projectApply = JSON.parseObject(data,ProjectApply.class); 
		String fileName = file.getOriginalFilename();
		Date d=new Date();
		SimpleDateFormat sf=new SimpleDateFormat("yyyyMMddhhmmss");
		if(file.getOriginalFilename() != ""){
			projectApply.setAttachment(projectApply.getUserid()+"_"+sf.format(d)+"_"+fileName);
		}
		//ProjectApplyVO projectApply = (ProjectApplyVO) JSON.parse(data);
		//int count = iProjectApplyService.insertSelective(projectApply);//iDeviceDocService.insertSelective(deviceDoc);
		int count = iProjectApplyService.updateByPrimaryKeySelective(projectApply);//iDeviceDocService.insertSelective(deviceDoc);
		String path = request.getSession().getServletContext().getRealPath("/");
		File targetFile = new File(path, projectApply.getUserid()+"_"+sf.format(d)+"_"+fileName);
		file.transferTo(targetFile); 
		
		
		JsonMessage jsonMessage = new JsonMessage();
		if(count == 1){
			jsonMessage.setKey("pass");
		}
		response.setContentType("text/html; charset=UTF-8");
		response.getWriter().write(JSON.toJSONString(jsonMessage));
	}
	@RequestMapping("/gotoxibushowallapply.do")
	public String gotoxibushowallapply(){
		return "/projectapply/xibushowallapply";
	}
	
	@RequestMapping("/updateprojectapply.do")
	public void updateprojectapply(@RequestBody ProjectApply projectApply,HttpServletResponse response) throws IOException{
		int count = iProjectApplyService.updateByPrimaryKeySelective(projectApply);
		JsonMessage jsonMessage = new JsonMessage();
		
		//以下是插入 专家 项目关联表
		if(projectApply.getExpertid() != null && projectApply.getExpertid().length > 0 ){
			ExpertAdvice expertAdvice = null;
			for (int i = 0; i < projectApply.getExpertid().length; i++) {
				expertAdvice = new ExpertAdvice();
				expertAdvice.setUserid(Integer.valueOf(projectApply.getExpertid()[i]));//设置专家id
				expertAdvice.setProjectapplayid(projectApply.getId());//项目id
				expertAdvice.setIsagree(0);//0表示不同意
				int id = iExpertAdviceService.insertSelective(expertAdvice);
			}
		}
		
		if(count == 1){
			jsonMessage.setKey("pass");
		}
		response.setContentType("text/html; charset=UTF-8");
		response.getWriter().write(JSON.toJSONString(jsonMessage));
	}
	
	@RequestMapping("/gotojiaowuchushowallapply.do")
	public String gotojiaowuchushowallapply(){
		return "/projectapply/jiaowuchushowallapply";
	}
	
	@RequestMapping("/updateexpertprojectapply.do")
	public void updateexpertprojectapply(@RequestBody ProjectApply projectApply,HttpServletResponse response) throws IOException{
		int count = iProjectApplyService.updateByPrimaryKeySelective(projectApply);
		JsonMessage jsonMessage = new JsonMessage();
		
		//以下是插入 专家 项目关联表
		if(projectApply.getExpertid() != null && projectApply.getExpertid().length > 0 ){
			ExpertAdvice expertAdvice = null;
			for (int i = 0; i < projectApply.getExpertid().length; i++) {
				expertAdvice = new ExpertAdvice();
				expertAdvice.setUserid(Integer.valueOf(projectApply.getExpertid()[i]));//设置专家id
				expertAdvice.setProjectapplayid(projectApply.getId());//项目id
				expertAdvice.setIsagree(0);//0表示不同意
				int id = iExpertAdviceService.insertSelective(expertAdvice);
			}
		}
		
		if(count == 1){
			jsonMessage.setKey("pass");
		}
		response.setContentType("text/html; charset=UTF-8");
		response.getWriter().write(JSON.toJSONString(jsonMessage));
	}
	
	/**
	 * 修改 查看项目申请书
	 * @param model
	 * @return
	 */
	@RequestMapping("/gotojiaowuchumodifyapply.do")
	public String gotojiaowuchumodifyapply(HttpServletRequest request, Model model){
		String ismodify = request.getParameter("ismodify");
		String id = request.getParameter("id");
		ProjectApply projectApply = iProjectApplyService.selectByPrimaryKey(Integer.valueOf(id));
		SysUser sysUser = iSysUserService.selectByPrimaryKey(projectApply.getUserid());
		
		List<Department> departments = iDepartmentService.getalldepartment();
		List<MoneySource> moneySources = iMoneySourceService.getallmoneysource();
		List<ProjectState> projectStates = iProjectStateService.getallprojectstates();
		
		model.addAttribute("sysuser", sysUser);
		model.addAttribute("departments", departments);
		model.addAttribute("moneysources", moneySources);
		model.addAttribute("projectstates", projectStates);
		model.addAttribute("projectapply", projectApply);

		if(ismodify.equals("yes")){ //修改
			model.addAttribute("ismodify", "yes");
		}else {//仅仅是
			model.addAttribute("ismodify", "chakan");
		}
		
		return "/projectapply/addapplyadmin";
	}
	
	@RequestMapping("/download.do")
	public void download(HttpServletResponse response,HttpServletRequest request, Model model) throws IOException{
		String filename = URLDecoder.decode(URLDecoder.decode(request.getParameter("file"),"UTF-8"),"UTF-8"); 
        //得到要下载的文件名
		String path = request.getSession().getServletContext().getRealPath("/");
		File file = new File(path, filename);
        response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8"));
        FileInputStream in = new FileInputStream(file);
        OutputStream out = response.getOutputStream();
        byte buffer[] = new byte[1024];
        int len = 0;
        while((len=in.read(buffer))>0){
            out.write(buffer, 0, len);
        }
        in.close();
        out.close();
	}
	
	/**
	 * 系部主任 审批 项目申请书
	 * @param model
	 * @return
	 */
	@RequestMapping("/gotoxibushenpiapply.do")
	public String gotoxibushenpiapply(HttpServletRequest request, Model model){
		String id = request.getParameter("id");
		ProjectApply projectApply = iProjectApplyService.selectByPrimaryKey(Integer.valueOf(id));
		SysUser sysUser = iSysUserService.selectByPrimaryKey(projectApply.getUserid());
		
		List<Department> departments = iDepartmentService.getalldepartment();
		List<MoneySource> moneySources = iMoneySourceService.getallmoneysource();
		List<ProjectState> projectStates = iProjectStateService.getallprojectstates();
		
		model.addAttribute("sysuser", sysUser);
		model.addAttribute("departments", departments);
		model.addAttribute("moneysources", moneySources);
		model.addAttribute("projectstates", projectStates);
		model.addAttribute("projectapply", projectApply);

		return "/projectapply/xibuaddapply";
	}
	
	/**
	 * 系部管理员
	 * @return
	 */
	@RequestMapping("/gotoxibuadminshowallapply.do")
	public String gotoxibuadminshowallapply(){
		return "/projectapply/xibuadminshowallapply";
	}
	
	/**
	 * 系部管理员 审批 项目申请书
	 * @param model
	 * @return
	 */
	@RequestMapping("/gotoxibumodifyapply.do")
	public String gotoxibumodifyapply(HttpServletRequest request, Model model){
		String id = request.getParameter("id");
		ProjectApply projectApply = iProjectApplyService.selectByPrimaryKey(Integer.valueOf(id));
		SysUser sysUser = iSysUserService.selectByPrimaryKey(projectApply.getUserid());
		
		List<Department> departments = iDepartmentService.getalldepartment();
		List<MoneySource> moneySources = iMoneySourceService.getallmoneysource();
		List<ProjectState> projectStates = iProjectStateService.getallprojectstates();
		
		model.addAttribute("sysuser", sysUser);
		model.addAttribute("departments", departments);
		model.addAttribute("moneysources", moneySources);
		model.addAttribute("projectstates", projectStates);
		model.addAttribute("projectapply", projectApply);

		return "/projectapply/xibuadminaddapply";
	}
	
	/**
	 * 系部管理员安排专家审批
	 * @return
	 */
	@RequestMapping("/gotoxibuadminexpertlookaddapply.do")
	public String gotoxibuadminexpertlookaddapply(HttpServletRequest request, Model model){
		String id = request.getParameter("id");
		ProjectApply projectApply = iProjectApplyService.selectByPrimaryKey(Integer.valueOf(id));
		SysUser sysUser = iSysUserService.selectByPrimaryKey(projectApply.getUserid());
		
		List<Department> departments = iDepartmentService.getalldepartment();
		List<MoneySource> moneySources = iMoneySourceService.getallmoneysource();
		List<ProjectState> projectStates = iProjectStateService.getallprojectstates();
		
		List<SysUserVO> experts = iSysUserService.getAllExpert(null);
		
		model.addAttribute("sysuser", sysUser);
		model.addAttribute("departments", departments);
		model.addAttribute("moneysources", moneySources);
		model.addAttribute("projectstates", projectStates);
		model.addAttribute("projectapply", projectApply);
		model.addAttribute("experts", experts);
		return "/projectapply/xibuadminexpertlookaddapply";
	}
	
	/**
	 * 查看专家评审结果
	 */
	@RequestMapping("/gotoseeexpertresult.do")
	public String gotoseeexpertresult(HttpServletRequest request, Model model){
		String id = request.getParameter("id");
		ProjectApply projectApply = iProjectApplyService.selectByPrimaryKey(Integer.valueOf(id));
		SysUser sysUser = iSysUserService.selectByPrimaryKey(projectApply.getUserid());
		
		List<Department> departments = iDepartmentService.getalldepartment();
		List<MoneySource> moneySources = iMoneySourceService.getallmoneysource();
		List<ProjectState> projectStates = iProjectStateService.getallprojectstates();
		
		List<SysUserVO> experts = iSysUserService.getAllExpert(null);
		
		Map<String, String> map = new HashMap<String, String>();
		map.put("id", id);
		List<ExpertAdviceVO> expertAdviceVOs = iExpertAdviceService.getExpertAdviceByProjectapplayid(map);
		
		model.addAttribute("sysuser", sysUser);
		model.addAttribute("departments", departments);
		model.addAttribute("moneysources", moneySources);
		model.addAttribute("projectstates", projectStates);
		model.addAttribute("projectapply", projectApply);
		model.addAttribute("experts", experts);
		model.addAttribute("expertAdviceVOs", expertAdviceVOs);
		
		double approve = 0;
		double waiting = 0;//未查看
		double disapprove = 0;
		for (ExpertAdviceVO expertAdviceVO : expertAdviceVOs) {
			if(expertAdviceVO.getIsagree() == 2){
				approve = approve + 1;
			}else if(expertAdviceVO.getIsagree() == 0){
				waiting = waiting + 1;
			}else {
				disapprove = disapprove + 1;
			}
		}
		//规则 1 人数大于等于一半专家人数 同意通过 
		//规则 2 如果人数小于一半 不可以通过
		//规则 3 如果人数小于一半 并且有一个状态是未查看 则显示等待按钮
		
		
		
		
		//人数大于一半 同意通过
		if(Double.valueOf(expertAdviceVOs.size())/2.00 < approve){
			model.addAttribute("approve", 2);
		}
		
		//不可以通过
		else if(Double.valueOf(expertAdviceVOs.size()) / 2.00 <= disapprove){
			model.addAttribute("approve", 1);
		}
		
		//不可以通过 并且 有一个是等待状态；不显示任何操作
		if(expertAdviceVOs.size() / 2 <= disapprove && waiting >= 1){
			model.addAttribute("approve", 0);
		}
		
		//等待
		if(expertAdviceVOs.size() == waiting ){
			model.addAttribute("approve", 0);
		}
		
		return "/projectapply/seeexpertresult";
	}
	
	/**
	 * 专家页面
	 * @return
	 */
	@RequestMapping("/gotoexpertpingshen.do")
	public String gotoexpertpingshen(){
		return "/projectapply/expertshowallapply";
	}
	
	/**
	 * 专家显示页面
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping("/expertshowallapply.do")
	public void expertshowallapply(HttpServletRequest request,HttpServletResponse response) throws IOException{
		String pageSize =String.valueOf(request.getParameter("rows"));
		String pageNum = String.valueOf(request.getParameter("page"));
		
		String applyername = request.getParameter("applyername");
		String projectstateid = request.getParameter("projectstateid");
		if(projectstateid != null && projectstateid.equals("-1")){
			projectstateid = null;
		}
		if(applyername != null){
			if(applyername.equals("")){
				applyername = null;
			}
		}
		
		
		Map<String, String> map = new HashMap<String, String>();
		map.put("pageSize", pageSize);
		map.put("pageNum", pageNum);
		map.put("applyername", applyername);
		map.put("projectstateid", projectstateid);

		Subject subject = SecurityUtils.getSubject();
		SysUser sysUser = iSysUserService.getUserByLoginName((String)subject.getPrincipal());
		
		map.put("id", String.valueOf(sysUser.getId()));
		
		List<ProjectApplyVO> projectApply = iProjectApplyService.getProjectApplyByExpertId(map);
		int count = iProjectApplyService.getProjectApplyCountByExpertId(map);
		
		SysPagingUtil sysPagingUtil = new SysPagingUtil();
		
		sysPagingUtil.setTotal(String.valueOf(count));
		sysPagingUtil.setRows(projectApply);

		String jsondata = JSON.toJSONString(sysPagingUtil);

		response.setContentType("html/json;charset=UTF-8");
		response.getWriter().write(jsondata);
	}
	
	/**
	 * 专家 审批 项目申请书
	 * @param model
	 * @return
	 */
	@RequestMapping("/gotoexpertmodifyapply.do")
	public String gotoexpertmodifyapply(HttpServletRequest request, Model model){
		String id = request.getParameter("id");
		ProjectApply projectApply = iProjectApplyService.selectByPrimaryKey(Integer.valueOf(id));
		
		Subject subject = SecurityUtils.getSubject();
		SysUser sysUser = iSysUserService.getUserByLoginName((String)subject.getPrincipal());
		
		List<Department> departments = iDepartmentService.getalldepartment();
		List<MoneySource> moneySources = iMoneySourceService.getallmoneysource();
		List<ProjectState> projectStates = iProjectStateService.getallprojectstates();
		
		Map<String, String> map = new HashMap<String, String>();
		map.put("projectapplayid", id);
		map.put("userid", String.valueOf(sysUser.getId()));
		ExpertAdvice expertAdvice = iExpertAdviceService.getExpertAdviceByExpertidAndProjectApplyid(map);
		
		model.addAttribute("sysuser", sysUser);
		model.addAttribute("departments", departments);
		model.addAttribute("moneysources", moneySources);
		model.addAttribute("projectstates", projectStates);
		model.addAttribute("projectapply", projectApply);
		model.addAttribute("expertAdvice", expertAdvice);

		return "/projectapply/expertaddapply";
	}
	
	/**
	 * 专家更新
	 * @param projectApply
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping("/updateexpertadvicetprojectapply.do")
	public void updateexpertadvicetprojectapply(@RequestBody ExpertAdvice expertAdvice,HttpServletResponse response) throws IOException{
		int count = iExpertAdviceService.updateexpertadvicebyexpertid(expertAdvice);
		JsonMessage jsonMessage = new JsonMessage();
		
		if(count == 1){
			jsonMessage.setKey("pass");
		}
		response.setContentType("text/html; charset=UTF-8");
		response.getWriter().write(JSON.toJSONString(jsonMessage));
	}
	
	/**
	 * 专家查看项目
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("/gotoexpertlookapply.do")
	public String gotoexpertlookapply(HttpServletRequest request, Model model){
		String id = request.getParameter("id");
		ProjectApply projectApply = iProjectApplyService.selectByPrimaryKey(Integer.valueOf(id));
		
		Subject subject = SecurityUtils.getSubject();
		SysUser sysUser = iSysUserService.getUserByLoginName((String)subject.getPrincipal());
		
		List<Department> departments = iDepartmentService.getalldepartment();
		List<MoneySource> moneySources = iMoneySourceService.getallmoneysource();
		List<ProjectState> projectStates = iProjectStateService.getallprojectstates();
		
		Map<String, String> map = new HashMap<String, String>();
		map.put("projectapplayid", id);
		map.put("userid", String.valueOf(sysUser.getId()));
		ExpertAdvice expertAdvice = iExpertAdviceService.getExpertAdviceByExpertidAndProjectApplyid(map);
		
		model.addAttribute("sysuser", sysUser);
		model.addAttribute("departments", departments);
		model.addAttribute("moneysources", moneySources);
		model.addAttribute("projectstates", projectStates);
		model.addAttribute("projectapply", projectApply);
		model.addAttribute("expertAdvice", expertAdvice);

		return "/projectapply/expertlookapply";
	}
}

